package com.ljw.food_order_server.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一返回类型
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {
    private Integer code;
    private String msg;
    private T data;

    private static final int SUCCESS_CODE = 0;
    private static final String SUCCESS_MSG = "SUCCESS";
    private static final int ERROR_CODE = 601;
    private static final String ERROR_MSG = "ERROR";

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R ok(T t){
        R r = new R();
        r.setCode(SUCCESS_CODE);
        r.setMsg(SUCCESS_MSG);
        r.setData(t);
        return r;
    }

    public static R getR(){
        return new R();
    }

    public static R ok(){
        R r = new R();
        r.setCode(SUCCESS_CODE);
        r.setMsg(SUCCESS_MSG);
        return r;
    }

    public static R error(){
        R r = new R();
        r.setCode(ERROR_CODE);
        r.setMsg(ERROR_MSG);
        return r;
    }

    public static R error(Integer code, String msg){
        return new R(code,msg);
    }


}
